<template>
  <div>
    <!-- 搜索框 -->
    <el-input v-model="searchQuery" placeholder="请输入用户名搜索" @keyup.enter="searchUsers" style="width: 200px; margin-bottom: 20px;"></el-input>
    <el-button @click="searchUsers">搜索</el-button>
    <!-- 添加用户按钮 -->
    <el-button type="primary" @click="openAddUserDialog">添加用户</el-button>

    <!-- 用户列表 -->
    <el-table :data="users" stripe>
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="isBlacklisted" label="是否黑名单">
        <template #default="scope">
          <el-switch v-model="scope.row.isBlacklisted" @change="toggleBlacklist(scope.row.id)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="warning" size="small" @click="openEditUserDialog(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加用户对话框 -->
    <el-dialog :visible.sync="addUserDialogVisible" title="添加用户">
      <template #content>
        <el-form :model="newUser" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="newUser.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="newUser.email"></el-input>
          </el-form-item>
          <el-form-item label="是否黑名单">
            <el-switch v-model="newUser.isBlacklisted"></el-switch>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button @click="addUserDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addUser">确定</el-button>
      </template>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog :visible.sync="editUserDialogVisible" title="编辑用户">
      <template #content>
        <el-form :model="editedUser" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="editedUser.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="editedUser.email"></el-input>
          </el-form-item>
          <el-form-item label="是否黑名单">
            <el-switch v-model="editedUser.isBlacklisted"></el-switch>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button @click="editUserDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updateUser">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';

// 模拟用户数据
const users = ref([
  { id: 1, username: 'user1', email: 'user1@example.com', isBlacklisted: false },
  { id: 2, username: 'user2', email: 'user2@example.com', isBlacklisted: true },
]);

// 搜索框输入
const searchQuery = ref('');
// 添加用户对话框可见性
const addUserDialogVisible = ref(false);
// 编辑用户对话框可见性
const editUserDialogVisible = ref(false);
// 新用户对象
const newUser = ref({ username: '', email: '', isBlacklisted: false });
// 编辑的用户对象
const editedUser = ref({ id: null, username: '', email: '', isBlacklisted: false });

// 搜索用户
const searchUsers = () => {
  // 这里可以实现搜索逻辑
  console.log('Searching for users with query:', searchQuery.value);
};

// 打开添加用户对话框
const openAddUserDialog = () => {
  addUserDialogVisible.value = true;
};

// 添加用户
const addUser = () => {
  const newId = users.value.length > 0 ? users.value[users.value.length - 1].id + 1 : 1;
  const newUserObj = { ...newUser.value, id: newId };
  users.value.push(newUserObj);
  newUser.value = { username: '', email: '', isBlacklisted: false };
  addUserDialogVisible.value = false;
};

// 打开编辑用户对话框
const openEditUserDialog = (user) => {
  editedUser.value = { ...user };
  editUserDialogVisible.value = true;
};

// 更新用户
const updateUser = () => {
  const index = users.value.findIndex(u => u.id === editedUser.value.id);
  if (index !== -1) {
    users.value[index] = { ...editedUser.value };
  }
  editUserDialogVisible.value = false;
};

// 删除用户
const deleteUser = (id) => {
  users.value = users.value.filter(u => u.id !== id);
};

// 切换黑名单状态
const toggleBlacklist = (id) => {
  const user = users.value.find(u => u.id === id);
  if (user) {
    user.isBlacklisted = !user.isBlacklisted;
  }
};
</script>

<style scoped>

</style>
